//
//  NicknameViewController.m
//  KIASupervisory
//
//  Created by apple on 15/8/9.
//  Copyright (c) 2015年 MyCompany. All rights reserved.
//
//
//  YpViewController.m
//  KIASupervisory
//
//  Created by apple on 15/7/19.
//  Copyright (c) 2015年 MyCompany. All rights reserved.
//

#import "NicknameViewController.h"
#import "GrzlViewController.h"
#import "MainViewController.h"
#import "SXDetailModel.h"
#import "SXDetailImgModel.h"
#import "SXHTTPManager.h"
@interface NicknameViewController ()

@property (nonatomic, strong) NSMutableArray *ypList;
@property (nonatomic, strong) UIButton *back;
@property (weak, nonatomic) IBOutlet UITextField *nicknameTxt;
@property (nonatomic,strong) SXDetailModel *detailModel;
@property (nonatomic, strong) NetworkManager *networkManager;
@property (nonatomic, strong) MBProgressHUD *HUD;

@end

@implementation NicknameViewController

@synthesize dataList = _dataList;
@synthesize showData = _showData;

- (void)viewDidLoad
{
    [self.navigationController setNavigationBarHidden:YES];
    self.networkManager = [[NetworkManager alloc] init];
    self.detailModel = [[SXDetailModel alloc] init];
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.HUD];
    self.HUD.delegate = self;
    [super viewDidLoad];
}

- (IBAction)backToPre:(id)sender {
//    GrzlViewController *problemVC = (GrzlViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"GrzlViewController"];
//    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
//    DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//    CATransition* transition = [CATransition animation];
//    transition.duration = 0.5;
//    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
//    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
//    [self.navigationController.view.layer addAnimation:transition forKey:nil];
//    [self.navigationController pushViewController:menuVC animated:NO];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)saveNickname:(id)sender {
    UserInfo *user = [UserInfo instance];
    if (self.networkManager == nil) {
        self.networkManager = [[NetworkManager alloc] init];
    }
    NSString *user_id = [user.info objectForKey:@"user_id"];
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                  self.nicknameTxt.text,@"nick_name",user_id,@"user_id", nil];
    
    [self.networkManager requestWithMethod:updateUserinfo parameter:param delegate:self];
    [self.HUD show:YES];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    NSString *nickname = self.nickname;
    
    NSString *unicodeStr = [nickname stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    self.nicknameTxt.text = unicodeStr;
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    
    if (methodId == updateUserinfo) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            if([result isEqualToString:@"000"]){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"保存成功"
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
                UserInfo *user = [UserInfo instance];
                [user.info setValue:self.nicknameTxt.text forKey:@"nickname"];
                
                GrzlViewController *problemVC = (GrzlViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"GrzlViewController"];
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
                DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
                [self.navigationController pushViewController:menuVC animated:YES];
            }
        }
        
    }
    
}

- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFailedWithError:(NSError *)error {
    [self.HUD hide:YES];
    
    NSDictionary *errorInfo = error.userInfo;
    NSString *msg = [errorInfo objectForKeyOrNil:@"errorMsg"];
    
    NSInteger code = error.code;
    if (code != 1) {
        if ([msg length] == 0) {
            msg = @"历史未完成问题获取失败，请稍后重试";
        }
    }
    
    if ([msg length] > 0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:msg
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
    }
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_showData count]>0?[_showData count]:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
                             TableSampleIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:TableSampleIdentifier];
    }
    if (_showData != nil && _showData.count >0) {
        NSUInteger row = [indexPath row];
        cell.textLabel.text = [_showData objectAtIndex:row];
    }
    return cell;
}

@end